﻿/*
培训任务的添加修改
*/
//
angular.module('jisuStuHome')
 .directive("addInterlocution", ["Msg", "$rootScope", 'Comm', "StuRequestTrain", "interlocutionservice", "sysUserInfo", "LS", function (Msg, $rootScope, Comm, ST, IS, sysUserInfo, LS) {
     return {
         restrice: "AE",
         scope: {
             rootItem: "=",
             uploader: "=",
             configbtnjishiben: "=", //配置富文本编辑器的按钮
             onClose: "&"    //关闭窗口        
         },
         templateUrl: "/member/directive/interlocution/add_interlocution.html",
         link: function (scope, element, attr) {

             scope.colInfo = LS.getLS(sysUserInfo.getuserinfo().user_ID + '_col'); //当前列表所属栏目对象
             scope.Exanimation = false;
             //查询审核开关
             scope.getSwitch = function () {
                 ST.getOrgSwitch({ switchs: "switchs_ask_questions", orgid: sysUserInfo.getuserinfo().organization_ID }).then(function (respose) {
                     if (respose.errorcode == "0") {
                         if (respose.data == undefined || respose.data.is_switch == undefined) {
                             scope.Exanimation = false;
                         } else if (respose.data.is_switch == 0) {
                             scope.Exanimation = true;
                         } else {
                             scope.Exanimation = false;
                         }
                     } else {
                         Msg.error("操作失败！");
                     }
                 }, function errorCallback() {
                     console.log("连接服务器失败！");
                 });
             }
             scope.getSwitch();
             //初始加载数据
             scope.init = function () {
                 //初始新增的参数
                 scope.item = {
                     columnId: "",
                     title: "", // 标题
                     content: "", // 内容
                     questionerId: sysUserInfo.getuserinfo().user_ID,
                     questionerName: sysUserInfo.getuserinfo().user_Name,
                     orgId: sysUserInfo.getuserinfo().organization_ID,
                     orgName: sysUserInfo.getuserinfo().organization_Name,
                     questionerAvatar: sysUserInfo.getuserinfo().user_Img,
                     isAnonymous: false, //是否匿名
                     spare: [], //附件JSON
                     xtype: 4, //4.专栏
                     xid: "",
                     xname: ""
                 };
                 if (scope.colInfo != undefined && scope.colInfo.columnId != undefined) {
                     scope.item.xid = scope.colInfo.columnId;
                     scope.item.xname = scope.colInfo.columnName;
                 }
                 scope.BeGoodAt = ""; //擅长领域
                 scope.beboodId = []; //选择的擅长领域
             }
             scope.init();
             /***********************************************专栏选择器********************************************************/
             //    配置知识选择器
             scope.netConfig = {   // 配置选择器的参数
                 menu: ["specialcolumn"],
                 scMore: false, //是否多选
                 scList: [],    //选中列表                 
                 isShow: false  // 显示选择器
             };
             // 打开知识选择器
             scope.opnetConfig = function () {
                 scope.netConfig.isShow = true;
             }
             //监听知识架构的关闭事件
             scope.$watch('netConfig.isShow', function (newV, oldV, scope) {
                 if (!scope.netConfig.isShow && scope.netConfig.scList.length > 0) {
                     scope.item.xid = scope.netConfig.scList[0].columnId;
                     scope.item.xname = scope.netConfig.scList[0].columnName;
                 }
             }, true);
             /*****************************************************************************************************************/
             //保存
             scope.saveObj = function () {
                 if (scope.item.title.length <= 0) {
                     Msg.warning("提示", "请输入标题！");
                     return;
                 }
                 if (scope.item.content.length <= 0) {
                     Msg.warning("提示", "请输入内容描述！");
                     return;
                 }
                 if (scope.item.xid.length <= 0) {
                     Msg.warning("提示", "请选择问题所属专栏！");
                     return;
                 }
                 var lasename = [];
                 //替换中文逗号
                 if (scope.classlistparam.searchText.length > 0) {
                     //替换中文逗号
                     scope.classlistparam.searchText = scope.classlistparam.searchText.replace(/，|\./g, ',');
                     scope.namelist = scope.classlistparam.searchText.split(",");

                     //剩下的名额
                     var lastindex = 5 - scope.checkClass.length;
                     if (lastindex > 0) {
                         scope.namelist.forEach(function (data, index) {
                             if (index < lastindex) {
                                 lasename.push(data);
                             }
                         })
                     }
                 }

                 //遍历选择的领域
                 var ids = [];
                 scope.checkClass.forEach(function (data, index) {
                     ids.push(data.cusId);
                 });
                 scope.item.spare = JSON.stringify(scope.item.spare);
                 //beboodat  自定义模板的名称
                 //beboodId  选择的自定义模板id
                 IS.saveEditQA({ data: JSON.stringify(scope.item), beboodat: lasename.join(","), beboodId: ids, colid: scope.item.xid }).then(function (respose) {
                     if (respose.errorcode == "0") {

                         if (scope.Exanimation) {
                             Msg.success("提问成功，需要管理员审核后可见，请耐心等待！");
                         } else {
                             scope.rootItem.askNum = scope.rootItem.askNum + 1;
                             Msg.success("添加成功！");
                             scope.TemplatePage(respose.data);
                         }
                         scope.closeForm();
                     } else {
                         Msg.error("操作失败！");
                     }
                 }, function errorCallback() {
                     console.log("连接服务器失败！");
                 });
             }
             //领域
             scope.openLable = function () {
                 scope.isShowLable = true;
             }
             scope.checkClass = [];
             scope.classAllList = [];
             //查询已存在领域的参数
             scope.classlistparam = {
                 orgid: sysUserInfo.getuserinfo().organization_ID,
                 searchText: ""
             }
             //选择领域
             scope.addLable = function (item) {
                 if (scope.checkClass.length >= 5) {
                     Msg.warning("提示", "提问领域不能超过五个！");
                     return;
                 }
                 var a = true;
                 scope.checkClass.forEach(function (data, index) {
                     if (data.cusId == item.cusId) {
                         a = false;
                         Msg.warning("提示", "该领域已存在！");
                     }
                 });
                 if (a) {
                     scope.checkClass.push(item);
                 }
                 scope.classlistparam.searchText = "";
             }
             //删除领域
             scope.delLable = function (item) {
                 scope.checkClass.forEach(function (data, index) {
                     if (data.cusId == item.cusId) {
                         scope.checkClass.splice(index, 1);
                     }
                 });
             }
             //查询已存在的领域列表
             scope.getClassAllList = function () {
                 IS.getClassAllList(scope.classlistparam).then(function (respose) {
                     if (respose.errorcode == "0") {
                         scope.classAllList = respose.datas;
                     } else {
                         Msg.error("查询分类失败！");
                     }
                 }, function errorCallback() {
                     console.log("连接服务器失败！");
                 });
             }
             //关闭页面
             scope.closeForm = function () {
                 scope.onClose();
             }
             // scope.getClassAllList();
             scope.wxdomain = "";
             //重新静态化网校的页面
             scope.TemplatePage = function (obj) {
                 scope.orginfo = sysUserInfo.getorganizationinfo();
                 if (!Comm.isNull(scope.orginfo.organization_domain_name)) {
                     scope.wxdomain = "//" + scope.orginfo.organization_domain_name;
                 } else if (!Comm.isNull(scope.orginfo.organization_two_domain_name)) {
                     scope.wxdomain = "//" + scope.orginfo.organization_two_domain_name + $rootScope.shortdomain;
                 }
                 if (!Comm.isNull(scope.wxdomain)) {
                     ST.template({ orgid: sysUserInfo.getuserinfo().organization_ID, qid: obj.qaid }).then(function (respose) { });
                     ST.qaTemplate({ orgId: sysUserInfo.getuserinfo().organization_ID, columnId: obj.xid }).then(function (respose) { });
                 }
             }
             /*********************************************文件上传开始**********************************************/

             scope.openFile = function () {
                 document.getElementById("fJButton").click();
             }
             var trainupfile = scope.uploader;
             trainupfile.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {     // 选择文件后失败       0
                 console.info('onWhenAddingFileFailed', item, filter, options);
             };
             trainupfile.onAfterAddingFile = function (fileItem) {  // 选择文件后 （成功）   0          
                 console.info('onAfterAddingFile', fileItem);
                 if (trainupfile.isUploading) {
                     Msg.warning("有文件正在上传。。");
                     return;
                 } else {
                     var upfileparam = "?orgid=" + sysUserInfo.getuserinfo().organization_ID + "&userid=" + sysUserInfo.getuserinfo().user_ID;
                     //上传文件路径
                     fileItem.url = ST.uploadEnclosure(upfileparam);
                 }
             };
             trainupfile.onAfterAddingAll = function (addedFileItems) { // 选择文件后添加到队列   1 
                 console.info('onAfterAddingAll', addedFileItems);
                 if (trainupfile.isUploading) {
                     Msg.warning("有文件正在上传。。");
                     return;
                 } else {
                     trainupfile.uploadAll();
                 }
             };
             // 开始上传
             trainupfile.onBeforeUploadItem = function (item) {    // 2 上传之前一项
                 console.info('onBeforeUploadItem', item);
             };
             trainupfile.onProgressItem = function (fileItem, progress) {   // 3 上传进度
                 console.info('onProgressItem', fileItem, progress);
             };
             trainupfile.onProgressAll = function (progress) {      //4 队列进度
                 console.info('onProgressAll', progress);
             };
             trainupfile.onSuccessItem = function (fileItem, response, status, headers) {   // 5上传成功
                 console.info('onSuccessItem', fileItem, response, status, headers); //fileItem 请求信息 response: 返回值status: 返回状态  headers: 请求头 
                 //                 scope.path = response.errormsg; //文件路径
                 //                 scope.filename = fileItem.name; //文件名称
                 if (response.errorcode == 0) {
                     scope.item.spare = [];
                     scope.item.spare.push({ name: fileItem.file.name, path: response.errormsg, type: fileItem.file.name.split(".")[1] });
                     Msg.success("上传成功！");
                 } else if (response.errorcode == "10002") {
                     Msg.warning("文件过大，请重新选择！");
                 } else {
                     Msg.error("上传失败！" + response.errorcode);
                 }
             };
             trainupfile.onErrorItem = function (fileItem, response, status, headers) {     // 5上传失败
                 console.info('onErrorItem', fileItem, response, status, headers);
             };
             trainupfile.onCancelItem = function (fileItem, response, status, headers) {    //5取消上传
                 console.info('onCancelItem', fileItem, response, status, headers);
             };
             trainupfile.onCompleteItem = function (fileItem, response, status, headers) {  //6     单文件上传完成
                 console.info('onCompleteItem', fileItem, response, status, headers);

             };
             trainupfile.onCompleteAll = function () {  // 6 单文件上传完成或者队列上传完成的时候
                 console.info('onCompleteAll');
             };
             /*********************************************文件上传结束**********************************************/
         }
     }
 } ]);
